//
//  WebVC.m
//  CommationApp
//
//  Created by iOS开发1 on 2018/10/26.
//  Copyright © 2018年 iOS开发1. All rights reserved.
//

#import "WebVC.h"
#import <WebKit/WebKit.h>

@interface WebVC ()

@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong) UIProgressView *progress;

@end

@implementation WebVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _topTitle;
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NavigationBar_Height )];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    if ([_urlString isEqualToString:@""]) { return; }
    NSString *urlStr = [_urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]]];
    [self.view addSubview:self.webView];
    
    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 3)];
    self.progress.trackTintColor = [UIColor whiteColor];
//    self.progress.progressTintColor = [UIColor crazy_hex:@"#5A82F5"];
    self.progress.progressTintColor = [UIColor colorWithHexString:@"#5A82F5"];
    [self.view addSubview:self.progress];

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"estimatedProgress"] && self.progress != nil) {
        self.progress.progress = self.webView.estimatedProgress;
        if (self.progress.progress == 1) {
            self.progress.hidden = YES;
        }
    }
    if ([keyPath isEqualToString:@"title"] && object == self.webView && !self.isNeedShowTitle) {
        self.title = self.webView.title;
    }
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    UIWebView *web = webView;
//    //获取所有的html
//    NSString *allHtmlInfo = [web stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
//    NSLog(@"%@",allHtmlInfo);
//    //获取网页的一个值
//    NSString *numHtmlInfo = [web stringByEvaluatingJavaScriptFromString:@"document.getElementById('title').innerText"];
//    NSLog(@"%@",numHtmlInfo);
//    //获取网页title
//    NSString *titleHtmlInfo = [web stringByEvaluatingJavaScriptFromString:@"document.title"];
//    self.title = titleHtmlInfo;
//}
//我不上班有人说，，我上了班还是有人说觉得好麻烦啊    现在面试的人挺多啊 就是不知到有什么哈哈哈哈哈哈哈哈哈哈哈你什么时候能

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView removeObserver:self forKeyPath:@"title"];
}


@end
